package operator;

import model.Book;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by haohao~ on 2015/12/12.
 */
public class BookDao {
    public static TreeMap<Long, Book> books = new TreeMap<Long, Book>();
    static Scanner scanner = new Scanner(System.in);

    public static void addBook() {
        System.out.println("请输入书名：");
        String name = scanner.next();
        System.out.println("请输入作者");
        String author = scanner.next();
        System.out.println("请输入出版社：");
        String publisher = scanner.next();
        System.out.println("请输入价格：");
        Double money = scanner.nextDouble();
        System.out.println("请输入书号：");
        Long id = scanner.nextLong();
        System.out.println("请输入出版日期");
        String date = scanner.next();
        Book book = new Book(name, author, publisher, money, id, date);

        System.out.println("新书信息如下：" + book);
        System.out.println("是否保存该书：（Y）是 （N）否");
        String choose = scanner.next();

        if ("y".equalsIgnoreCase(choose)) {
            books.put(id, book);
            UserDao.userLogin();
        } else if ("n".equalsIgnoreCase(choose)) {
            addBook();
        }

    }

    public static void viewBook(TreeMap books) {
        System.out.println("|书名|" + "\t" + "\t" + "|作者|" + "\t" + "|出版社|" + "\t" + "\t" +
                "|价格|" + "\t" + "|书号|" + "\t" + "|出版日期|" + "\t");

        Set<Map.Entry<Long, Book>> entrys = books.entrySet();
        Iterator<Map.Entry<Long, Book>> it = entrys.iterator();
        
        while (it.hasNext()) {
            Map.Entry<Long, Book> entry = it.next();
            Object o = entry.getValue();
            Book book = (Book)o;

            System.out.println(book.getName() + "\t" + "\t" + book.getAuthor() + "\t" + "\t" + book.getPublisher() +
                    "\t" + book.getMoney() + "\t" + book.getId() + "\t" + book.getDate());
        }

        System.out.println("请选择查看方式：（A）价格排序  （B）作者排序  (C)出版日期排序  （E）返回上一级");

        Scanner s = new Scanner(System.in);

        if ("a".equalsIgnoreCase(s.next())) {
            sortBy(new SortByMoney());
        }

        if ("b".equalsIgnoreCase(s.next())) {
            sortBy(new SortByAuthor());
        }

        if ("c".equalsIgnoreCase(s.next())) {
            sortBy(new SortByDate());
        }

        if ("e".equalsIgnoreCase(s.next())) {
            UserDao.userLogin();
        }
    }

    public static void viewSortBook(TreeMap books) {
        System.out.println("|书名|" + "\t" + "\t" + "|作者|" + "\t" + "|出版社|" + "\t" + "\t" +
                "|价格|" + "\t" + "|书号|" + "\t" + "|出版日期|" + "\t");

        Set<Map.Entry<Book, Long>> entrys = books.entrySet();
        Iterator<Map.Entry<Book, Long>> it = entrys.iterator();

        while (it.hasNext()) {
            Map.Entry<Book, Long> entry = it.next();
            Book book = entry.getKey();

            System.out.println(book.getName() + "\t" + "\t" + book.getAuthor() + "\t" + "\t" + book.getPublisher() +
                    "\t" + book.getMoney() + "\t" + book.getId() + "\t" + book.getDate());
        }

        System.out.println("请选择查看方式：（A）价格排序  （B）作者排序  (C)出版日期排序  （E）返回上一级");

        Scanner s = new Scanner(System.in);

        if ("a".equalsIgnoreCase(s.next())) {
            sortBy(new SortByMoney());
        }

        if ("b".equalsIgnoreCase(s.next())) {
            sortBy(new SortByAuthor());
        }

        if ("c".equalsIgnoreCase(s.next())) {
            sortBy(new SortByDate());
        }

        if ("e".equalsIgnoreCase(s.next())) {
            UserDao.userLogin();
        }
    }

    public static void updateBook(){
        Scanner s = new Scanner(System.in);
        Book book = null;
        Long bookId = null;

        while (true) {
            System.out.println("请输入要修改的书号：");
            bookId = s.nextLong();
            if (books.containsKey(bookId)) {
                book = books.get(bookId);
                break;
            } else {
                System.out.println("没有这个书号的书，请重新输入");
                continue;
            }
        }

        System.out.println(book);
        System.out.println("请输入要个性的属性：书名修改（A） 作者修改（B） 出版社修改（C） 价格修改（D） 书号修改（E）" +
                "出版日期修改（F） 返回上一级（G）");
        String choose = s.next();

        if ("a".equalsIgnoreCase(choose)) {
            //书名修改；
            updateBookName(bookId,book);
        } else if ("b".equalsIgnoreCase(choose)) {
            //作者修改
            updateBookAuthor(bookId, book);
        } else if ("c".equalsIgnoreCase(choose)) {
            //出版社修改
            updateBookPublisher(bookId, book);
        } else if ("d".equalsIgnoreCase(choose)) {
            //价格修改
            updateBookMoney(bookId, book);
        } else if ("e".equalsIgnoreCase(choose)) {
            //书号修改
            updateBookId(bookId, book);
        } else if ("f".equalsIgnoreCase(choose)) {
            //出版日期修改
            updateBookDate(bookId, book);
        } else if ("g".equalsIgnoreCase(choose)) {
            UserDao.userLogin();
        }
    }

    public static void updateBookName(Long bookId, Book book) {
        System.out.println("请输入新的书名：");
        Scanner s = new Scanner(System.in);
        String name = s.next();
        System.out.println("你将" + book.getName() + "更换为：" + name +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setName(name);
            books.put(bookId, book);
            System.out.println("修改成功！");
            viewBook(books);
        } else if ("n".equalsIgnoreCase(choose)) {
            viewBook(books);
        }

    }

    public static void updateBookAuthor(Long bookId, Book book) {
        System.out.println("请输入新的作者：");
        Scanner s = new Scanner(System.in);
        String author = s.next();
        System.out.println("你将" + book.getAuthor() + "更换为：" + author +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setAuthor(author);
            books.put(bookId, book);
            System.out.println("修改成功！");
            viewBook(books);
        } else if ("n".equalsIgnoreCase(choose)) {
            viewBook(books);
        }
    }

    public static void updateBookPublisher(Long bookId, Book book) {
        System.out.println("请输入新的出版社：");
        Scanner s = new Scanner(System.in);
        String publisher = s.next();
        System.out.println("你将" + book.getPublisher() + "更换为：" + publisher +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setPublisher(publisher);
            books.put(bookId, book);
            System.out.println("修改成功！");
            viewBook(books);
        }
    }

    public static void updateBookMoney(Long bookId, Book book) {
        System.out.println("请输入新的价格：");
        Scanner s = new Scanner(System.in);
        Double money = s.nextDouble();
        System.out.println("你将" + book.getMoney() + "更换为：" + money +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setMoney(money);
            books.put(bookId, book);
            System.out.println("修改成功！");
            viewBook(books);
        }
    }

    public static void updateBookId(Long bookId, Book book) {
        System.out.println("请输入新的书号：");
        Scanner s = new Scanner(System.in);
        Long id = s.nextLong();
        System.out.println("你将" + book.getId() + "更换为：" + id +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setId(id);
            books.remove(bookId);
            books.put(id, book);
            System.out.println("修改成功！");
            viewBook(books);
        }
    }

    public static void updateBookDate(Long bookId, Book book) {
        System.out.println("请输入新的日期：");
        Scanner s = new Scanner(System.in);
        String  date = s.next();
        System.out.println("你将" + book.getDate() + "更换为：" + date +
                "确定吗？（Y/N）");
        String choose = s.next();

        if ("y".equalsIgnoreCase(choose)) {
            book.setDate(date);
            books.put(bookId, book);
            System.out.println("修改成功！");
            viewBook(books);
        }
    }

    public static void removeBook() {
        Scanner s = new Scanner(System.in);
        Set<Map.Entry<Long, Book>> entries = books.entrySet();
        Iterator<Map.Entry<Long, Book>> it = entries.iterator();

        System.out.println("请输入要删除书的书名：");
        String name = s.next();

        while (it.hasNext()) {
            Map.Entry<Long,Book> book=it.next();

            if (name.equals(book.getValue().getName())) {
                Long bookId = book.getKey();
                System.out.println(book);
                System.out.println("确定要删除吗？（Y/N）");
                String choose=s.next();
                if ("y".equalsIgnoreCase(choose)) {
                    if (books.remove(bookId, book.getValue())) {
                        System.out.println("删除成功！");
                        viewBook(books);
                    } else {
                        System.out.println("删除失败！");
                    }
                } else if ("n".equalsIgnoreCase(choose)) {
                    viewBook(books);
                }
            }
        }
    }

    private static void sortBy(Comparator c) {
        TreeMap<Book, Long> newBooks = null;

        if (c instanceof SortByAuthor) {
            SortByAuthor comparator = (SortByAuthor) c;
            newBooks = new TreeMap<Book, Long>(comparator);
        } else if (c instanceof SortByDate) {
            SortByDate comparator = (SortByDate) c;
            newBooks = new TreeMap<Book, Long>(comparator);
        } else if (c instanceof SortByMoney) {
            SortByMoney comparator = (SortByMoney) c;
            newBooks = new TreeMap<Book, Long>(comparator);
        } else {
            SortById comparator = (SortById) c;
            newBooks = new TreeMap<Book, Long>(comparator);
        }

        Set<Map.Entry<Long, Book>> entrys = books.entrySet();
        Iterator<Map.Entry<Long, Book>> it = entrys.iterator();

        while (it.hasNext()) {
            Map.Entry<Long, Book> entry = it.next();
            Book book = entry.getValue();
            Long l = entry.getKey();
            newBooks.put(book, l);
        }

        viewSortBook(newBooks);

    }

    static class SortById implements Comparator<Book> {
        @Override
        public int compare(Book o1, Book o2) {
            return (int) (o1.getId() - o2.getId());
        }
    }

    static class SortByMoney implements Comparator<Book> {
        @Override
        public int compare(Book o1, Book o2) {
            return (int) (o1.getMoney() - o2.getMoney());
        }
    }

    static class SortByAuthor implements Comparator<Book> {
        @Override
        public int compare(Book o1, Book o2) {
            return o1.getAuthor().compareTo(o2.getAuthor());
        }
    }

    static class SortByDate implements Comparator<Book> {
        @Override
        public int compare(Book o1, Book o2) {
            SimpleDateFormat sForm = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = null;
            Date d2 = null;
            try {
                d1 = sForm.parse(o1.getDate());
                d2 = sForm.parse(o2.getDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return d1.compareTo(d2);
        }
    }
}


